package com.youlai.system.controller.device;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.common.result.Result;
import com.youlai.system.mqtt.MqttSubClient;
import com.youlai.system.pojo.entity.device.WxMacRelEntity;
import com.youlai.system.pojo.form.MacForm;
import com.youlai.system.pojo.query.MacQuery;
import com.youlai.system.service.impl.device.WxMacRelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "10.设备预警日志接口")
@RestController
@RequestMapping("/api/v1/device/gpsMac")
@RequiredArgsConstructor
public class GpsMacController {
    private final WxMacRelService wxMacRelService;

    @Autowired
    private MqttSubClient mqttSubClient;

    @GetMapping("/reRestart")
    public Result<Boolean> reRestart(String mac){
        Map<String,String> data = Maps.newHashMap();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("rest_742", "yes");
        mqttSubClient.send("/down/rest/" + mac, jsonObject.toJSONString());
        return Result.success(Boolean.TRUE);
    }

    @PostMapping("/addOrUpdate")
    @Operation(summary = "添加或者更新预警", security = {@SecurityRequirement(name = "Authorization")})
    public Result<Boolean> addOrUpdate(@RequestBody MacForm macForm) {

        if (macForm.getId() == null) {
            LambdaQueryWrapper<WxMacRelEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(WxMacRelEntity::getMac, macForm.getMac());

            List<WxMacRelEntity> wxMacRelEntities = wxMacRelService.list(lambdaQueryWrapper);
            if (CollectionUtils.isNotEmpty(wxMacRelEntities)) {
                macForm.setId(wxMacRelEntities.get(0).getId());
            }

        }
        WxMacRelEntity wxMacRelEntity = new WxMacRelEntity();
        BeanUtils.copyProperties(macForm, wxMacRelEntity);
        wxMacRelService.saveOrUpdate(wxMacRelEntity);
        Map<String,String> data = Maps.newHashMap();
        data.put("ip_address",wxMacRelEntity.getServer());
        data.put("port_number",wxMacRelEntity.getPort().toString());
        mqttSubClient.send("/down/tcp_ip/"+wxMacRelEntity.getMac(),
                JSON.toJSONString(data));
        return Result.success(Boolean.TRUE);
    }

    @GetMapping("/delete/{id}")
    @Operation(summary = "删除", security = {@SecurityRequirement(name = "Authorization")})
    public Result<Boolean> delete(@PathVariable Integer id) {
        wxMacRelService.removeById(id);
        return Result.success(Boolean.TRUE);
    }
    @GetMapping("/{id}/form")
    @Operation(summary = "预警详情", security = {@SecurityRequirement(name = "Authorization")})
    public Result<WxMacRelEntity> detail(@PathVariable Integer id) {
        WxMacRelEntity wxMacRelEntity = wxMacRelService.getById(id);
        return Result.success(wxMacRelEntity);
    }

    @Operation(summary = "设备预警配置分页列表", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/page")
    public PageResult<WxMacRelEntity> page(MacQuery query) {

        LambdaQueryWrapper<WxMacRelEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(query.getMac())){
            lambdaQueryWrapper.eq(WxMacRelEntity::getMac,query.getMac());
        }

        lambdaQueryWrapper.orderByDesc(WxMacRelEntity::getId);
        Page<WxMacRelEntity> page = new Page(query.getPageNum(), query.getPageSize());
        page = wxMacRelService.page(page, lambdaQueryWrapper);


        return PageResult.success(page);
    }


}
